package com.kinghunt.misp.web.controller.providex;

import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.providex.ProvideDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.common.param.providex.ProvideParam;
import com.kinghunt.misp.service.api.providex.ProvideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;

/**
 * 供应商管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@RestController
@RequestMapping("/api/provide/")
public class ProvideController {

    @Autowired(required = false)
    private ProvideService provideService;

    /**
     * 列表显示：显示供应商列表
     */
    @GetMapping("getProvideList")
    public ApiResult<PageInfo<ProvideDTO>> getProvideList(ProvideParam provideParam) {
        return provideService.getProvideList(provideParam);
    }
    /**
     * 供应商详情展示
     */
    @GetMapping("getProvideDetail")
    public ApiResult<ProvideDTO> getProvideDetail(Long provideId) {
        if(Objects.isNull(provideId)){
            return ApiResult.failOfMessage("查询失败，请输入供应商信息！");
        }
        return provideService.getProvideDetail(provideId);
    }
    /**
     * 新增供应商
     */
    @PostMapping(value = "addProvide", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult addProvide(@Valid @RequestBody ProvideParam provideParam) {
        if(Objects.isNull(provideParam.getProvideNo())){
            return ApiResult.failOfMessage("新增失败，请输入供应商编号！");
        }
        UserDTO loginUser = getCurrentUser();
        provideParam.setCreatorId(loginUser.getId());
        provideParam.setUpdaterId(loginUser.getId());
        return provideService.addProvide(provideParam);
    }
    /**
     * 修改供应商
     */
    @PostMapping(value = "updateProvide", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult updateProvide(@Valid @RequestBody ProvideParam provideParam) {
        UserDTO loginUser = getCurrentUser();
        provideParam.setUpdaterId(loginUser.getId());
        return provideService.updateProvide(provideParam);
    }
    /**
     * 删除供应商
     *
     * @param provideId
     * @return 是否成功
     */
    @DeleteMapping("deleteProvide")
    public ApiResult<Boolean> deleteProvide(Long provideId) {
        if(Objects.isNull(provideId)){
            return ApiResult.failOfMessage("删除失败，请输入供应商信息！");
        }
        return provideService.deleteProvide(provideId);
    }

}
